<template>
  <div>
    <el-button size="small" @click="handleOpen">
      <i class="el-icon-plus fw-6 mr-5"></i>
      {{ title }}
    </el-button>
    <div class="flex flexwarp mt-12">
      <div v-for="(item, index) in keywordList" :key="index" class="tagitem mr-10 line_h_22">
        {{ item }}
      </div>
    </div>
    <el-dialog
      title="关键词设置"
      :visible.sync="show"
      width="500px"
      :before-close="handleClose"
      append-to-body
      class="groupdia"
    >
      <div class="c_666 mb-12">最多设置500个关键词，按换行(Enter)键间隔</div>
      <el-form ref="form" :model="form">
        <el-form-item label="">
          <el-input v-model="form.keyword" type="textarea" rows="6"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button size="small" @click="show = false">取 消</el-button>
        <el-button size="small" type="primary" @click="handleSubmit">确 定</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      show: false,
      keywordList: [],
      form: {
        keyword: "",
      },
    };
  },
  props: {
    title: {
      type: String,
      default: "设置关键词",
    },
    value: {
      type: Array,
      default: [],
    },
  },
  watch: {
    value: {
      handler(val) {
        this.keywordList = val;
      },
      immediate: true,
      deep: true,
    },
  },
  methods: {
    handleOpen() {
      if(this.keywordList.length){
        let value = ''
        this.keywordList.forEach(item=>{
            value += item+'\n'
        })
        console.log(value)
        this.form.keyword = value
      }
      this.show = true;
    },

    handleClose() {
      this.show = false;
    },

    // handleKeydown(e) {
    //   // 回车键
    // //   if (e.keyCode == 13) {
    // //     e.cancelBubble = true; //ie阻止冒泡行为
    // //     e.stopPropagation(); //Firefox阻止冒泡行为
    // //     // e.preventDefault(); //取消事件的默认动作*换行
    // //     return false;
    // //   }
    // },

    handleSubmit() {
      console.log('this.form.keyword:', this.form.keyword)
      if (this.form.keyword.trim()) {
        this.keywordList = this.form.keyword.split(/[(\r\n)\r\n]+/).filter((item) => item !== "");
        this.handleClose()
        this.$emit("input", this.keywordList);
      }
    },
  },
};
</script>
<style lang="scss" scoped>
.tagitem {
  background: #fafafa;
  border: 1px solid #e9e9e9;
  border-radius: 4px;
  padding: 2px 7px;
}
</style>